import java.util.concurrent.*;

public class Counter {

    static class Result{
        public int sum = 0;
        public Object lock = new Object();
    }

    public static void main6(String[] args) {
        Semaphore semaphore = new Semaphore(4);
        Runnable runnable = new Runnable() {
      @Override
      public void run() {
            try {
                  System.out.println("申请资源");
                  semaphore.acquire();
                  System.out.println("我获取到资源了");
                  Thread.sleep(1000);
                  System.out.println("我释放资源了");
                  semaphore.release();
          } catch (InterruptedException e) {
                  e.printStackTrace();
          }
    }
        };
        for (int i = 0; i < 20; i++) {
      Thread t = new Thread(runnable);
      t.start();
        }
    }

    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(4);
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(Thread.currentThread().getName()+ "申请资源");
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName()+ "获得了资源");
                    Thread.sleep(1000);
                    System.out.println(Thread.currentThread().getName()+ "释放了资源");
                    semaphore.release();
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        };

        for (int i = 0; i < 20; i++) {
            Thread t = new Thread(runnable, "Thread"+i);
            t.start();
        }


    }



    public static void main3(String[] args) {
        
        //        ExecutorService pool = new ThreadPoolExecutor(1, 2, 1000, TimeUnit.MILLISECONDS,
//                new SynchronousQueue<Runnable>(), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
//        for (int i=0; i<3; i++){
//            pool.submit(new Runnable() {
//                @Override
//                public void run() {
//                    System.out.println("hello");
//                }
//            });
//        }
    }

    public static void main2(String[] args) throws ExecutionException, InterruptedException { // Callable 版本写法完成 线程计算结果，并且返回

        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int sum = 0;
                for (int i = 0; i <= 1000; i++) {
                    sum += i;
                }
                return sum;
            }
        };

        FutureTask<Integer> futureTask = new FutureTask<>(callable);
        Thread t = new Thread(futureTask);
        t.start();

        int result = futureTask.get();
        System.out.println(result);

    }

    public static void main1(String[] args) {
        Result result = new Result();
        Thread t = new Thread(){       // 这种写法相当于是重写了 Thread 类中的run方法
          public void run(){
              int sum = 0;
              for (int i = 0; i <= 1000; i++) {
                  sum += i;
              }
              synchronized (result.lock){
                  result.sum = sum;
                  result.lock.notify();
              }
          }
        };

        t.start();
        synchronized (result.lock){
            while(result.sum == 0){
                System.out.println("等待");
                try {
                    result.lock.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        System.out.println(result.sum);
    }


}
